package com.autumn.zero.workflow.services.impl;

import org.activiti.bpmn.model.FlowElement;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.springframework.beans.factory.annotation.Autowired;

import com.autumn.zero.workflow.services.IListenerService;
import com.autumn.zero.workflow.services.IProcessService;

/**
 * 
 * 
 * @author 杨昌国 2019-01-24 14:19:34
 */
public class ListenerServiceImpl implements IListenerService {

	@Autowired
	private IProcessService processService;

	@Override
	public void executionListenerNotify(DelegateExecution execution) {
		FlowElement flowElement = execution.getCurrentFlowElement();
		if (flowElement != null) {
			if (flowElement.getName() != null) {
				// 流程启动
				if (flowElement.getName().equalsIgnoreCase("start")
						&& execution.getEventName().equalsIgnoreCase("start")) {
					processService.startProcessEventSave(execution);
				}
				// 流程结束
				if (flowElement.getName().equalsIgnoreCase("end")
						&& execution.getEventName().equalsIgnoreCase("start")) {
					processService.endProcessEventSave(execution);
				}
			}
		}
	}

	@Override
	public void taskListenerNotify(DelegateTask delegateTask) {
		if ("assignment".equalsIgnoreCase(delegateTask.getEventName())) {
			processService.userTaskWaitEvent(delegateTask);
		} else if ("complete".equalsIgnoreCase(delegateTask.getEventName())) {
			processService.userTaskCompleteEvent(delegateTask);
		} else if ("delete".equalsIgnoreCase(delegateTask.getEventName())) {
			processService.userTaskDeleteEvent(delegateTask);
		}
	}
}
